package com.per.other.until;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


import org.apache.commons.logging.Log;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.tomcat.util.codec.binary.StringUtils;

/**
 * 日期时间工具类
 * 
 * @author gong
 * 
 */
public class DateTimeUtil {
	//private static Log logger = InfConfig.logger;
	private static Logger logger = Logger.getLogger("interface");
	/**
	 * 时间戳格式:yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String getTimeStampString() {
		return getTimeStamp(new Date(), null);
	}

	public static Timestamp getTimeStamp() {
		return string2Timestamp(getTimeStampString());
	}

	public static String getTimeStamp(Date date) {
		return getTimeStamp(date, null);
	}

	public static String getTimeStamp(String format) {
		return getTimeStamp(new Date(), format);
	}

	public static String getTimeStamp(Date date, String format) {
		if (format == null) {
			format = "yyyyMMddHHmmss";
		}
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		return fmt.format(date);
	}

	public final static java.sql.Timestamp string2Timestamp(String dateString) {
		if (dateString == null || "".equals(dateString)) {
			return null;
		}
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		dateFormat.setLenient(false);
		java.util.Date date = null;
		try {
			date = dateFormat.parse(dateString);
		} catch (ParseException e) {
			logger.error(e);
		}
		return new java.sql.Timestamp(date.getTime());
	}

	public static String timestampToString(Timestamp timestamp) {
		if(null == timestamp){
			return "";
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String str = df.format(timestamp);
		return str;
	}
	
	public static String timesToString(Timestamp timestamp) {
		if(timestamp == null){
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		String str = df.format(timestamp);
		return str;
	}
	
	public static String dateStringFormat(String dateString,String format)
	{
		
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		dateFormat.setLenient(false);
		java.util.Date date = null;
		try {
			date = dateFormat.parse(dateString);
			return getTimeStamp(date, format);
		} catch (ParseException e) {
			logger.error(e);
			return "";
		}
	}
	
	public static java.sql.Timestamp stringToTimestamp(String dateString){
		if (dateString == null || "".equals(dateString)) {
			return null;
		}
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat("yyyyMMdd");
		dateFormat.setLenient(false);
		java.util.Date date = null;
		try {
			date = dateFormat.parse(dateString);
		} catch (ParseException e) {
			logger.error(e);
		}
		return new java.sql.Timestamp(date.getTime());
	}

	/**
	 * 获得昨天时间，格式自定义
	 * 
	 * @param format
	 * @return
	 */
	public static String getYesterdayDate(String format) {
		Calendar day = Calendar.getInstance();
		day.add(Calendar.DATE, -1);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String date = sdf.format(day.getTime());
		return date;
	}

	/**
	 * 获得今天时间，格式自定义
	 * 
	 * @param format
	 * @return
	 */
	public static String getTodayDate(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String date = sdf.format(new Date());
		return date;
	}

	public static Timestamp getYesterdayZeroClock() {
		return string2Timestamp(getYesterdayDate("yyyyMMdd") + "000000");
	}

	public static Timestamp getTodayZeroClock() {
		return string2Timestamp(getTodayDate("yyyyMMdd") + "000000");
	}

	public static String timesstampToDateString(Timestamp timestamp, String format) {
		Date date = new Date(timestamp.getTime());
		return getTimeStamp(date, format);
	}

	public static int getHour() {
		//return new Date().getHours();
		return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
	}
	
	public static Date string2Date(String strDate, String format){
		DateFormat dateFormat = new SimpleDateFormat(format);
		java.util.Date date = null;
		try {
			date = dateFormat.parse(strDate);
		} catch (ParseException e) {
			logger.error(e);
		}
		return date;
	}

	public static void main(String[] args) {
		System.out.println(string2Date("2014-08-22 16:03","yyyy-MM-dd HH:mm"));
//		//System.out.println(getTodayZeroClock());
		// //System.out.println(timestampToString(getTodayZeroClock()));
		//PropertyConfigurator.configure("resource/log4j.properties");
		//logger.info(DateTimeUtil.getHour());
	}

}
